package br.ufrn.dimap.ase.squid.metrics.views.tree.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IProject;

import br.ufrn.dimap.ase.squid.metrics.actions.GranularityMetricsAction;
import br.ufrn.dimap.ase.squid.metrics.actions.IRunableMetrics;
import br.ufrn.dimap.ase.squid.metrics.actions.LocalizationMetricsAction;
import br.ufrn.dimap.ase.squid.metrics.actions.TamanhoMetricsAction;
import br.ufrn.dimap.ase.squid.metrics.actions.TransversalityMetricsAction;
import br.ufrn.dimap.ase.squid.metrics.views.tree.model.exception.NoChildrensException;

public class Projeto implements ITreeable {
	private IProject project;
	
	public Projeto(IProject iProject) {
		this.project = iProject;
	}

	public Feature[] getFeatures() throws NoChildrensException {
		try {
			IRunableMetrics action = new GranularityMetricsAction();
			action.init(project);
			Set<String> featuresSet = action.getMappings().keySet();
			
			List<Feature> features = new ArrayList<Feature>();
			for(String featureNome : featuresSet){
				Feature feature = new Feature();
				feature.setNome(featureNome);
				
				Conjunto cGranularidade = new Conjunto(project, "Granularidade");
				Conjunto cLocalizacao = new Conjunto(project, "Localizacao");
				Conjunto cTamanho = new Conjunto(project, "Tamanho");
				Conjunto cTransversalidade = new Conjunto(project, "Transversalidade");
				
				action = new GranularityMetricsAction();
				cGranularidade.setResultados( action.run(project, featureNome) );
				
				action = new LocalizationMetricsAction();
				cLocalizacao.setResultados( action.run(project, featureNome) );
				
				action = new TamanhoMetricsAction();
				cTamanho.setResultados( action.run(project, featureNome) );
				
				action = new TransversalityMetricsAction();
				cTransversalidade.setResultados( action.run(project, featureNome) );
				
				
				Conjunto[] conjuntos = new Conjunto[]{
						cGranularidade,
						cLocalizacao,
						cTamanho,
						cTransversalidade
				};
				
				feature.setConjuntos(conjuntos);
				
				features.add(feature);
			}
			
			Feature[] featuresArray = new Feature[features.size()];
			
			for (int i = 0; i < features.size(); i++) {
				featuresArray[i] = features.get(i);
			}
			
			return featuresArray;
		} catch (Exception e) {
			throw new NoChildrensException(e);
		}
	}

	@Override
	public Object[] getChildrens() throws NoChildrensException {
		return this.getFeatures();
	}

	public String toString() {
		return this.project.getName();
	}

}
